package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.util.DBUtil;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderdetailetDaoImpl implements OrderdetailetDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public int saveOrderdetailet(List<Orderdetailet> list) throws SQLException {
        int result = 0;
        Cart cart = new Cart();
        CartDao cartDao = new CartDemoImpl();
        String sql = "INSERT INTO orderdetailet(orderId,foodId,quantity) VALUES";
        StringBuffer s = new StringBuffer(sql);
        for (int k=0;k<list.size();k++) {
            Orderdetailet orderdetailet = list.get(k);
            s.append("(" + orderdetailet.getOrderId() + "," + orderdetailet.getFoodId() + "," + orderdetailet.getQuantity() + ")" + ",");
        }
        String substring = s.substring(0, s.length() - 1);

        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(substring);

            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public List<Orderdetailet> listOrderdetailet(Integer orderId) throws SQLException {
        List<Orderdetailet> list = new ArrayList<>();
        String sql = "SELECT * FROM orderdetailet,food WHERE orderdetailet.foodId = food.foodId AND orderId = ?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);

            pst.setInt(1,orderId);

            rs = pst.executeQuery();

            while (rs.next()){

                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));

                Orderdetailet orderdetailet = new Orderdetailet();
                orderdetailet.setOdId(rs.getInt("odId"));
                orderdetailet.setOrderId(rs.getInt("orderId"));
                orderdetailet.setFoodId(rs.getInt("foodId"));
                orderdetailet.setQuantity(rs.getInt("quantity"));

                orderdetailet.setFood(food);

                list.add(orderdetailet);

            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return list;
    }
}
